/*
 * Copyright (C) 2009 Chang Min Lee <ch5ngmn@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details. (/COPYING)
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
#include "CommBrokerConfiguration.h"
#include "ace/OS.h"

CommBrokerConfiguration::CommBrokerConfiguration ()
{
	args_.add (ACE_TEXT ("dummy"));
}

void CommBrokerConfiguration::enableDebug()
{
	args_.add (ACE_TEXT ("-ORBDebug"));
}
	
void CommBrokerConfiguration::setDebugLevel (ACE_UINT32 level)
{
	ACE_OS::memset (debugLevel_, '\0', sizeof (debugLevel_));
	ACE_OS::sprintf (debugLevel_, ACE_TEXT ("-ORBDebugLevel %u"), level);
	args_.add (debugLevel_);
}

void CommBrokerConfiguration::setLogFile (const ACE_TCHAR *file)
{
	ACE_OS::memset (logFile_, '\0', sizeof (logFile_));
	ACE_OS::sprintf (logFile_, ACE_TEXT ("-ORBLogFile %s"), file);
	args_.add (logFile_);
}

/////////////////////////
// Request Processing
void CommBrokerConfiguration::setCollocationType (CollocationType type)
{
	switch (type)
	{
	case Global :
		args_.add (ACE_TEXT ("-ORBCollocation global"));
		break;
	case Per_ORB:
		args_.add (ACE_TEXT ("-ORBCollocation per-orb"));
		break;
	case No:
		args_.add (ACE_TEXT ("-ORBCollocation no"));
		break;
	}
}

void CommBrokerConfiguration::setCollocatoinStrategy (CollocationStrategy strategy)
{
	switch (strategy)
	{
	case ThruPoa :
		args_.add (ACE_TEXT ("-ORBCollocationStrategy ThruPoa"));
		break;
	case Direct:
		args_.add (ACE_TEXT ("-ORBCollocationStrategy Direct"));
		break;

	}
}

void CommBrokerConfiguration::setNoDelay (bool delay)
{
	if (delay)
		args_.add (ACE_TEXT ("-ORBNoDelay 1"));
	else
		args_.add (ACE_TEXT ("-ORBNoDelay 0"));
}

void CommBrokerConfiguration::setSendSock (size_t size)
{
	ACE_OS::memset (sendSock_, '\0', sizeof (sendSock_));
	ACE_OS::sprintf (sendSock_, ACE_TEXT ("-ORBRcvSock %d"), size);
	args_.add (sendSock_);
}

void CommBrokerConfiguration::setRecvSock (size_t size)
{
	ACE_OS::memset (recvSock_, '\0', sizeof (recvSock_));
	ACE_OS::sprintf (recvSock_,ACE_TEXT ("-ORBRcvSock %d"), size);
	args_.add (recvSock_);
}

void CommBrokerConfiguration::singleReadOptimization (bool enabled)
{
	if (enabled)
		args_.add (ACE_TEXT ("-ORBSingleReadOptimization 1"));
	else
		args_.add (ACE_TEXT ("-ORBSingleReadOptimization 0"));
}

//////////////////////
// Connection Management

void CommBrokerConfiguration::setLingerTimeout (int timeout)
{
	ACE_OS::memset (lingerTimeout_, '\0', sizeof (lingerTimeout_));
	ACE_OS::sprintf (lingerTimeout_,ACE_TEXT ("-ORBLingerTimeout %d"), timeout);
	args_.add (lingerTimeout_);
}

void CommBrokerConfiguration::setListenEndpoint (const ACE_TCHAR *hostname, ACE_UINT16 port, ACE_UINT16 portspan)
{
	ACE_OS::memset (listenEndpoint_, '\0', sizeof (listenEndpoint_));
	ACE_OS::sprintf (listenEndpoint_, 
		ACE_TEXT ("-ORBListenEndpoints iiop://%s:%u/portspan=%u"), hostname, port, portspan);
	args_.add (listenEndpoint_);
}

	
void CommBrokerConfiguration::nameService (const ACE_TCHAR *file)
{
	ACE_OS::memset (nameService_, '\0', sizeof (nameService_));
	//ACE_OS::sprintf (nameService_, ACE_TEXT ("-ORBInitRef NameService=file://%s"), file);
	ACE_OS::sprintf (nameService_, 
		ACE_TEXT ("-ORBInitRef NameService=corbaloc:iiop:192.168.10.8:12345/NameService"));
	
	args_.add (nameService_);

}

void CommBrokerConfiguration::nameService (const ACE_TCHAR *ip, ACE_UINT16 port)
{
	ACE_OS::memset (nameService_, '\0', sizeof (nameService_));
	ACE_OS::sprintf (nameService_, 
		ACE_TEXT ("-ORBInitRef NameService=corbaloc:iiop:%s:%u/NameService"), ip, port);
	
	args_.add (nameService_);

}


void CommBrokerConfiguration::dump () 
{
  for (int i = 0; i < args_.argc(); ++i)
    ACE_DEBUG ((LM_DEBUG, ACE_TEXT ("argv[%02d] = %s\n"), i, args_.argv ()[i]));
}